import {
  createRouter,
  createWebHashHistory
} from 'vue-router'

const router = createRouter({
  history: createWebHashHistory(), // hash模式：createWebHashHistory，history模式：createWebHistory
  routes: [{
      path: '/',
      redirect: '/home'
    },
    {
      path: '/government/home',
      redirect: '/government'
    },
    {
      path: '/home',
      name: 'home',
      component: () => import( /* webpackChunkName: "home" */ '@/views/Home.vue'),
      meta: {
        index: 1
      }
    },
    {
      path: '/login-farmer',
      name: 'LoginFarmer',
      component: () => import( /* webpackChunkName: "home" */ '@/views/LoginFarmer.vue'),
      meta: {
        index: 1
      }
    },
    {
      path: '/login-government',
      name: 'LoginGovernment',
      component: () => import( /* webpackChunkName: "home" */ '@/views/LoginGovernment.vue'),
      meta: {
        index: 1
      }
    },
    // 我的
    {
      path: '/user',
      name: 'user',
      component: () => import( /* webpackChunkName: "user" */ '@/views/User.vue'),
      meta: {
        index: 1
      }
    },
    // 宅基地情况
    {
      path: '/build-apply/apply-base',
      name: 'ApplyBase',
      component: () => import('@/views/build-apply/ApplyBase.vue')
    },
    // 申请人员信息
    {
      path: '/build-apply/apply-info',
      name: 'ApplyInfo',
      component: () => import('@/views/build-apply/ApplyInfo.vue')
    },
    // 申请须知
    {
      path: '/build-apply/apply-notes',
      name: 'ApplyNotes',
      component: () => import('@/views/build-apply/ApplyNotes.vue')
    },
    // 编辑家庭成员信息
    {
      path: '/build-apply/edit-info',
      name: 'EditInfo',
      component: () => import('@/views/build-apply/EditInfo.vue')
    },
    // 申报材料上传
    {
      path: '/build-apply/apply-materials',
      name: 'ApplyMaterials',
      component: () => import('@/views/build-apply/ApplyMaterials.vue')
    },
    // 建房信息
    {
      path: '/build-apply/build-info',
      name: 'BuildInfo',
      component: () => import('@/views/build-apply/BuildInfo.vue')
    },
    // 开工申请
    {
      path: '/start-apply',
      name: 'StartApply',
      component: () => import('@/views/start-apply/index.vue')
    },
    // 竣工申请
    {
      path: '/finish-apply',
      name: 'FinishApply',
      component: () => import('@/views/finish-apply/index.vue')
    },
    // 申请记录
    {
      path: '/record-apply/record-list',
      name: 'RecordList',
      component: () => import('@/views/record-apply/RecordList.vue')
    },
    // 建房申请记录 
    {
      path: '/record-apply/detail-build/:id',
      name: 'BuildApplyRecord',
      component: () => import('@/views/record-apply/BuildApplyRecord.vue')
    },
    // 开工申请记录 
    {
      path: '/record-apply/detail-start/:id',
      name: 'StartApplyRecord',
      component: () => import('@/views/record-apply/StartApplyRecord.vue')
    },
    // 竣工申请记录 
    {
      path: '/record-apply/detail-finish/:id',
      name: 'FinishApplyRecord',
      component: () => import('@/views/record-apply/FinishApplyRecord.vue')
    },

    // 公示栏
    {
      path: '/notice-board',
      name: 'NoticeBoard',
      component: () => import('@/views/notice-board/index.vue')
    },
    // 公示详情
    {
      path: '/notice-board/detail/:id',
      name: 'NoticeBoardDetail',
      component: () => import('@/views/notice-board/Detail.vue'),
    },
    // 异议提交
    {
      path: '/notice-board/dissent',
      name: 'NoticeBoardDissent',
      component: () => import('@/views/notice-board/Dissent.vue'),
    },
    // 投诉举报
    {
      path: '/user/complaint',
      name: 'UserComplaint',
      component: () => import('@/views/user/complaint/index.vue'),
    },
    // 投诉举报新增
    {
      path: '/user/complaint/add',
      name: 'UserComplaintAdd',
      component: () => import('@/views/user/complaint/Add.vue'),
    },
    // 投诉举报详情
    {
      path: '/user/complaint/detail/:id',
      name: 'UserComplaintDetail',
      component: () => import('@/views/user/complaint/Detail.vue'),
    },
    // 我的异议
    {
      path: '/user/public-dissent',
      name: 'UserPublicDissent',
      component: () => import('@/views/user/public-dissent/index.vue'),
    },
    // 我的异议详情
    {
      path: '/user/public-dissent/detail/:id',
      name: 'UserPublicDissentDetail',
      component: () => import('@/views/user/public-dissent/Detail.vue'),
    },
    // 结果页面
    {
      path: '/results',
      name: 'ResultsPage',
      component: () => import('@/views/ResultsPage.vue'),
    },
    // ==========================================  政务端  ==========================================
    // 首页
    {
      path: '/government',
      name: 'GovernmentHome',
      component: () => import('@/views/government/Home.vue'),
      meta: {
        index: 1
      }
    },
    // 绑定
    {
      path: '/government/Bind',
      name: 'GovernmentBind',
      component: () => import('@/views/government/Bind.vue'),
      meta: {
        index: 1
      }
    },
    // 我的
    {
      path: '/government/user',
      name: 'GovernmentUser',
      component: () => import('@/views/government/user/index.vue'),
      meta: {
        index: 1
      }
    },
    // 投诉举报
    {
      path: '/government/user/complaint',
      name: 'GovernUserComplaint',
      component: () => import('@/views/government/user/complaint/index.vue'),
    },
    // 投诉举报新增
    {
      path: '/government/user/complaint/add',
      name: 'GovernUserComplaintAdd',
      component: () => import('@/views/government/user/complaint/Add.vue'),
    },
    // 投诉举报详情
    {
      path: '/government/user/complaint/detail/:id',
      name: 'GovernUserComplaintDetail',
      component: () => import('@/views/government/user/complaint/Detail.vue'),
    },
    // 宅基地情况
    {
      path: '/government/build-apply/apply-base',
      name: 'GovernApplyBase',
      component: () => import('@/views/government/build-apply/ApplyBase.vue')
    },
    // 申请人员信息
    {
      path: '/government/build-apply/apply-info',
      name: 'GovernApplyInfo',
      component: () => import('@/views/government/build-apply/ApplyInfo.vue')
    },
    // 申请须知
    {
      path: '/government/build-apply/apply-notes',
      name: 'GovernApplyNotes',
      component: () => import('@/views/government/build-apply/ApplyNotes.vue')
    },
    // 编辑家庭成员信息
    {
      path: '/government/build-apply/edit-info',
      name: 'GovernEditInfo',
      component: () => import('@/views/government/build-apply/EditInfo.vue')
    },
    // 申报材料上传
    {
      path: '/government/build-apply/apply-materials',
      name: 'GovernApplyMaterials',
      component: () => import('@/views/government/build-apply/ApplyMaterials.vue')
    },
    // 建房信息
    {
      path: '/government/build-apply/build-info',
      name: 'GovernBuildInfo',
      component: () => import('@/views/government/build-apply/BuildInfo.vue')
    },
    // 建房申请详情
    {
      path: '/government/build-apply/detail/:id',
      name: 'GovernBuildApplyDetail',
      component: () => import('@/views/government/build-apply/Detail.vue'),
    },
    // 公示栏
    {
      path: '/government/notice-board',
      name: 'GovernNoticeBoard',
      component: () => import('@/views/government/notice-board/index.vue')
    },
    // 公示详情
    {
      path: '/government/notice-board/detail/:id',
      name: 'GovernNoticeBoardDetail',
      component: () => import('@/views/government/notice-board/Detail.vue'),
    },
    // 异议提交
    {
      path: '/government/notice-board/dissent',
      name: 'GovernNoticeBoardDissent',
      component: () => import('@/views/government/notice-board/Dissent.vue'),
    },
    // 审查公示
    {
      path: '/government/review-public',
      name: 'GovernReviewPublic',
      component: () => import('@/views/government/review-public/index.vue')
    },
    // 编辑审查项
    {
      path: '/government/review-item',
      name: 'GovernReviewItem',
      component: () => import('@/views/government/ReviewItem.vue')
    },
    // 审查环节
    {
      path: '/government/review-public/examine/:id',
      name: 'ReviewPublicExamine',
      component: () => import('@/views/government/review-public/Examine.vue'),
    },
    // 公示环节
    {
      path: '/government/review-public/publicity/:id',
      name: 'ReviewPublicPublicity',
      component: () => import('@/views/government/review-public/Publicity.vue'),
    },
    // 部门审批
    {
      path: '/government/bulid-dept',
      name: 'GovernBulidDept',
      component: () => import('@/views/government/bulid-dept/index.vue')
    },
    // 部门审批办理
    {
      path: '/government/bulid-dept/transact/:id',
      name: 'BulidDeptTransact',
      component: () => import('@/views/government/bulid-dept/Transact.vue'),
    },
    // 乡镇联审  
    {
      path: '/government/joint-review',
      name: 'GovernJointReview',
      component: () => import('@/views/government/joint-review/index.vue')
    },
    // 乡镇联审办理
    {
      path: '/government/joint-review/transact/:id',
      name: 'JointReviewTransact',
      component: () => import('@/views/government/joint-review/Transact.vue'),
    },
    // 乡镇联审办理-反馈详情
    {
      path: '/government/joint-review/feedback',
      name: 'JointReviewFeedback',
      component: () => import('@/views/government/joint-review/Feedback.vue'),
    },
    // 开工审批
    {
      path: '/government/start-work',
      name: 'StartWork',
      component: () => import('@/views/government/start-work/index.vue'),
    },
    { // 开工审批办理
      path: '/government/start-work/handle',
      name: 'StartWorkHandle',
      component: () => import('@/views/government/start-work/Handle.vue'),
    },
    // 施工现场监管
    {
      path: '/government/construction',
      name: 'ConstructionList',
      component: () => import('@/views/government/construction/index.vue'),
    },
    {
      path: '/government/construction/detail',
      name: 'ConstructionDetail',
      component: () => import('@/views/government/construction/Detail.vue'),
    },
    {
      path: '/government/construction/record',
      name: 'ConstructionRecord',
      component: () => import('@/views/government/construction/Record.vue'),
    },
    {
      path: '/government/construction/recordAdd',
      name: 'ConstructionRecordAdd',
      component: () => import('@/views/government/construction/RecordAdd.vue'),
    },
    {
      path: '/government/construction/handle',
      name: 'ConstructionHandle',
      component: () => import('@/views/government/construction/Handle.vue'),
    },
    // 竣工审批
    {
      path: '/government/finish-work',
      name: 'FinishWork',
      component: () => import('@/views/government/finish-work/index.vue'),
    },
    { // 竣工审批办理
      path: '/government/finish-work/transact/:id',
      name: 'FinishWorkTransact',
      component: () => import('@/views/government/finish-work/Transact.vue'),
    },
    // 开工申请
    {
      path: '/government/start-apply',
      name: 'GovernStartApply',
      component: () => import('@/views/government/start-apply/index.vue')
    },
    // 竣工申请
    {
      path: '/government/finish-apply',
      name: 'GovernFinishApply',
      component: () => import('@/views/government/finish-apply/index.vue')
    },
    // 申请记录
    {
      path: '/government/record-apply/record-list',
      name: 'GovernRecordList',
      component: () => import('@/views/government/record-apply/RecordList.vue')
    },
    // 建房申请记录 
    {
      path: '/government/record-apply/detail-build/:id',
      name: 'GovernBuildApplyRecord',
      component: () => import('@/views/government/record-apply/BuildApplyRecord.vue')
    },
    // 开工申请记录 
    {
      path: '/government/record-apply/detail-start/:id',
      name: 'GovernStartApplyRecord',
      component: () => import('@/views/government/record-apply/StartApplyRecord.vue')
    },
    // 竣工申请记录 
    {
      path: '/government/record-apply/detail-finish/:id',
      name: 'GovernFinishApplyRecord',
      component: () => import('@/views/government/record-apply/FinishApplyRecord.vue')
    },
    // 结果页面
    {
      path: '/government/results',
      name: 'GovernResultsPage',
      component: () => import('@/views/government/ResultsPage.vue'),
    },
  ]
})

router.beforeEach((to, from, next) => {
  const auth = ['/login-government', '/login-farmer']
  if (auth.includes(to.fullPath)) {
    next()
  } else {
    let href = window.location.href
    let token = sessionStorage.getItem('token') || ''
    if (href.includes('government')) {
      token = sessionStorage.getItem('govern_token') || ''
    }
    if (token) {
      next()
    } else {
      if (to.fullPath.includes('government')) {
        next('/login-government')
      } else {
        next('/login-farmer')
      }
    }
  }
})

export default router